<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/User.php';
require_once 'CMS/Language.php'; //include thu vien language

//Người tạo : Vũ Hoàng Khanh
//Chỉnh sửa : Bùi Thành Nguyên
//Module 	: Category
//Chức năng	: Show sản phẩm theo danh mục
 
class Category_IndexController{

	public function init(){
		parent::init();	
	}

//Action index thực hiện chức năng xử lý dữ liệu đăng ký
    public function indexAction(){
	
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/category/');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
		
		/*========== Start Get Text ==============*/
			$this->view->textWarning1 	= $language->get('warning1');
			$this->view->textError2 	= $language->get('error2');
			$this->view->textError3 	= $language->get('error3');
			$this->view->textError12 	= $language->get('error12');
			$this->view->textError13 	= $language->get('error13');
			$this->view->textDelsuccess = $language->get('text_deletesuccess');
		/*========== End Text ==============*/
		
		//Start--------Khai báo các parameter
		$tbCategory= new Category_Model_Categories();
		$path = APPLICATION_PATH."/widgets/category/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		//End--------Khai báo các parameter
		
		//Start------Get Categories
		$category=$tbCategory->getAllCategories();
		$type=0;
   		 if(count($_GET) && isset($_GET["type"]))//nếu người dùng chọn một category
		{
			$type=$_GET["type"];
			$pcategory=$tbCategory->getParentCategoriesbyType($type);
			$lcategory=$tbCategory->getLeafCategoriesbyType($type);
			
		}
		else
		{
			
			$pcategory=$tbCategory->getParentProductCategories();
			$lcategory=$tbCategory->getLeafProductCategories();
		}
		switch ($_GET["type"])
		{
			case 1: $this->view->type = 'index';break;
			case 2: $this->view->type = 'news';break;
			case 3: $this->view->type = 'qa';break;
			default : $this->view->type = 'index';
		}
		//get name of category
		$newPCate = array();
		foreach ($pcategory as $pcate){
			$nameCate = unserialize($pcate['category_name']);
			if(isset($nameCate[$name['name']]) && isset($name)){
				$category_name = $nameCate[$name['name']];
			}else{
				$category_name = $nameCate['Default'];
			}
			$newPCate[] = array(
									'category_id'	=>	$pcate['category_id'],
									'category_code'	=>	$pcate['category_code'],
									'category_name'	=>	$category_name,
									'parent_id'		=>	$pcate['parent_id'],
									'is_leaf'		=>	$pcate['is_leaf'],
									'type'			=>	$pcate['type']
								);
		}
    	$newLCate = array();
		foreach ($lcategory as $lcate){
			$nameCate = unserialize($lcate['category_name']);
			if(isset($nameCate[$name['name']]) && isset($name)){
				$category_name = $nameCate[$name['name']];
			}else{
				$category_name = $nameCate['Default'];
			}
			$newLCate[] = array(
									'category_id'	=>	$lcate['category_id'],
									'category_code'	=>	$lcate['category_code'],
									'category_name'	=>	$category_name,
									'parent_id'		=>	$lcate['parent_id'],
									'is_leaf'		=>	$lcate['is_leaf'],
									'type'			=>	$lcate['type']
								);
		}		
		//End--------Get category
		
		//Start------Truyền parameter qua View
			/************ Start check user login *********/
		$session1 = new Zend_Session_Namespace('login.auth');
		if(isset($session1->id_user))
		{
			$this->view->login = "1";
		
			/************ Start check permission *********/
			$user = new User();
			$user->init();
			$hasmodify = $user->modifiy_widget($xmlconfig->name);
			$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else 
		{
			
			$this->view->login = "0";
		}
		/************ End check user login *********/
		$this->view->category=$category;
		$this->view->Pprocategory=$newPCate;
		$this->view->Lprocategory=$newLCate;
		$titleXML = 'title'.$name['name'];
		if(isset($xmlconfig->$titleXML))
			$this->view->title = $xmlconfig->$titleXML;
		else
        	$this->view->title = $xmlconfig->titleDefault;
		//End--------Truyền parameter qua View
		
    }	
}